﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using Entities;


namespace DataAccessLayer
{
   public class KhachHangDAL
   {
       QLNhaSach.DataService ds = new QLNhaSach.DataService();

       public DataTable layThongTinKhachHang(int maKhachHang)
       {           
           DataTable DT = new DataTable();
           try
           {
               string sql = String.Format("SELECT * FROM KhachHang where KH_MaKhachHang = {0}", maKhachHang);
               DT = ds.GetTable(sql);
               ds.Close();
           }
           catch (Exception)
           {
               throw;
           }
           return DT;
       }
       public DataTable layDanhSachKhachHang()
       {
           DataTable DT = new DataTable();
           try
           {
               string sql = "SELECT * FROM KhachHang" ;
               DT = ds.GetTable(sql);
           }
           catch (Exception)
           {
               throw;
           }
           return DT;
       }
       public int InsertKhachHang(KhachHang kh)
       {
           int kq = 0;
           try
           {
               String sql = "INSERT INTO KHACHHANG (KH_HOTENKHACHHANG,KH_DIACHI,KH_DIENTHOAI,KH_EMAIL,KH_TIENNO) VALUES ('" + kh.HoTenKhachHang + "',' " + kh.DiaChi + "','" + kh.DienThoai + "','"+kh.Email+"',"+kh.TienNo+")";
               kq = ds.ExcuteInsert(sql);
           }
           catch (Exception)
           {
               throw;
           }
           return kq;

       }
       public int DeleteKhachHang(string maKhachHang)
       {
           int kq = 0;
           try
           {
               String sql = "DELETE FROM KHACHHANG WHERE KH_MAKHACHHANG=" + maKhachHang;
               kq = int.Parse(ds.ExcuteInsert(sql).ToString());
           }
           catch (Exception)
           {
               throw;
           }
           return kq;

       }
       public int UpdateKhachHang(KhachHang kh)
       {
           int kq = 0;
           try
           {
               String sql = "UPDATE KHACHHANG SET KH_HOTENKHACHHANG='" + kh.HoTenKhachHang + "', KH_DIACHI='" + kh.DiaChi + "',KH_DIENTHOAI='" + kh.DienThoai + "',KH_EMAIL='" + kh.Email+ "',KH_TIENNO=" + kh.TienNo+ " WHERE KH_MAKHACHHANG=" + kh.MaKhachHang;
               kq = int.Parse(ds.ExcuteInsert(sql).ToString());
           }
           catch (Exception)
           {
               throw;
           }
           return kq;

       }
    }
}
